﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace _7DaysToDieTools.Class
{
    /// <summary>
    /// ZIP压缩解压
    /// </summary>
    public static class ZipAction
    {
        /// <summary>
        /// 解压数据
        /// </summary>
        /// <param name="str"></param>
        /// <param name="UnPath"></param>
        /// <returns></returns>
        public static bool UnzipDatas(Stream str,string UnPath)
        {
            using (ZipInputStream zis = new ZipInputStream(str))
            {
                ZipEntry ze;
                while ((ze = zis.GetNextEntry()) != null)
                {
                    if (ze.IsDirectory)
                    {
                        Directory.CreateDirectory(UnPath + "\\" + ze.Name);
                    }
                    else if (ze.IsFile)
                    {
                        using (FileStream fs = File.Create(UnPath + "\\" + ze.Name))
                        {
                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = zis.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    fs.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }

                return true;
        }
    }
}
